package util;

import sniff.pack.CommonIPChildPacket;
import sniff.pack.CommonIPPacket;
import sniff.pack.CommonPacket;
import sniff.pack.MyARPPacket;
import sniff.pack.MyTCPPacket;
import sniff.pack.MyUDPPacket;
import ui.widget.ShowPromptDialog;

public class SearchUtil {
	public static boolean isTcpPortCmd(String[] param, CommonPacket pack) {
		if (pack.getType().equals("IPV4") || pack.getType().equals("IPV6")) {
			CommonIPPacket ipPack = (CommonIPPacket) pack;
			if (ipPack.getProtocol().equals("TCP")) {
				MyTCPPacket tcpPack = (MyTCPPacket) ipPack.getChildPacket();
				if (param == null || param.length == 0) {
					ShowPromptDialog.showMessage("tcp.port:端口参数是空的");
					return false;
				} else {
					for (String port : param) {
						int pport = Integer.valueOf(port);
						if (tcpPack.getSourctPort() == pport || tcpPack.getDestinationPort() == pport) {
							return true;
						}
					}
				}
			}
		}
		return false;
	}

	public static boolean isUdpPortCmd(String[] param, CommonPacket pack) {
		if (pack.getType().equals("IPV4") || pack.getType().equals("IPV6")) {
			CommonIPPacket ipPack = (CommonIPPacket) pack;
			if (ipPack.getProtocol().equals("UDP")) {
				MyUDPPacket udpPack = (MyUDPPacket) ipPack.getChildPacket();
				if (param == null || param.length == 0) {
					ShowPromptDialog.showMessage("udp.port:端口参数是空的");
				} else {
					for (String port : param) {
						
						int pport = Integer.valueOf(port);
						if (udpPack.getSourctPort() == pport || udpPack.getDestinationPort() == pport) {
							return true;
						}
					}
				}
			}
		}
		return false;
	}

	public static boolean isOnlyProcotolCmd(String cmd, CommonPacket pack) {
		if (pack.getType().toUpperCase().equals(cmd)) {
			return true;
		} else {
			if (pack.getType().equals("IPV4") || pack.getType().equals("IPV6")) {
				CommonIPPacket ipPack = (CommonIPPacket) pack;
				if (ipPack.getProtocol().equals(cmd)) {
					return true;
				} else {
					if (ipPack.getProtocol().equals("TCP")) {
						CommonIPChildPacket ipCPack = ipPack.getChildPacket();
						if (ipCPack instanceof MyTCPPacket) {
							MyTCPPacket tcpPack = (MyTCPPacket) ipCPack;
							if (tcpPack.getChildPacket() != null && cmd.equals("HTTP")) {
								return true;
							}
						} else {
							System.out.println("error in searchBtnActionEvent TCP");
						}
					}
				}
			}
		}
		return false;
	}

	public static boolean isIpAddrCmd(String[] param, CommonPacket pack) {
		return isIpv4AddrCmd(param, pack) || isIpv4AddrCmd(param, pack);
	}

	public static boolean isIpv4AddrCmd(String[] param, CommonPacket pack) {
		return ipAddrCmd("IPV4", param, pack);
	}

	public static boolean isIpv6AddrCmd(String[] param, CommonPacket pack) {
		return ipAddrCmd("IPV6", param, pack);
	}

	private static boolean ipAddrCmd(String type, String[] param, CommonPacket pack) {
		if (pack.getType().equals(type)) {
			CommonIPPacket ipPack = (CommonIPPacket) pack;
			String destIP = ipPack.getDestinationIP();
			String sourIP = ipPack.getSourceIP();
			if (param == null || param.length == 0) {
				ShowPromptDialog.showMessage("ip.port:端口参数是空的");
			} else {
				for (String pa : param) {
					if (pa.trim().equals(destIP) || pa.trim().equals(sourIP)) {
						return true;
					}
				}
			}
		}
		return false;
	}

	public static boolean isArpMacCmd(String[] param, CommonPacket pack) {
		if (pack.getType().equals("ARP")) {
			MyARPPacket arpPack = (MyARPPacket) pack;
			String sendMac = arpPack.getSenderMac();
			String tarMac = arpPack.getTargetMac();
			if (param == null || param.length == 0) {
				ShowPromptDialog.showMessage("arp.mac:mac地址参数是空的");
			} else {
				for (String pa : param) {
					if (pa.trim().equals(sendMac) || pa.trim().equals(tarMac)) {
						return true;
					}
				}
			}
		}
		return false;
	}

	public static boolean isArpIpCmd(String[] param, CommonPacket pack) {
		if (pack.getType().equals("ARP")) {
			MyARPPacket arpPack = (MyARPPacket) pack;
			String sendIP = arpPack.getSenderIP();
			String tarIP = arpPack.getTargetIP();
			if (param == null || param.length == 0) {
				ShowPromptDialog.showMessage("arp.ip:ip参数是空的");
			} else {
				for (String pa : param) {
					if (pa.trim().equals(sendIP) || pa.trim().equals(tarIP)) {
						return true;
					}
				}
			}
		}
		return false;
	}

	public static boolean isMacCmd(String[] param, CommonPacket pack) {
		String destMac = pack.getDestinationMAC();
		String sourMac = pack.getSourceMAC();
		if (param == null || param.length == 0) {
			ShowPromptDialog.showMessage("mac:mac地址参数是空的");
		} else {
			for (String pa : param) {
				if (pa.trim().equals(destMac) || pa.trim().equals(sourMac)) {
					return true;
				}
			}
		}
		return false;
	}
}
